// 引入js-cookie库
import Cookies from 'js-cookie'

const TOKEN_KEY = 'sc_token'
const USER_KEY = 'sc_user_info'
const IMPACT_TYPE_KEY = 'sc_impact_type'
const IMPACT_CODE_KEY ='sc_impact_code'

// 设置认证token到cookie
export function setToken(token, expires = 7) {
  Cookies.set(TOKEN_KEY, token, { expires: expires })
}

// 从cookie中获取认证token
export function getToken() {
  return Cookies.get(TOKEN_KEY)
}

// 移除认证token
export function removeToken() {
  Cookies.remove(TOKEN_KEY)
}

// 设置用户信息到cookie
export function setUserInfo(userInfo, expires = 7) {
  Cookies.set(USER_KEY, JSON.stringify(userInfo), { expires: expires })
}

// 从cookie中获取用户信息
export function getUserInfo() {
  const userInfo = Cookies.get(USER_KEY)
  return userInfo ? JSON.parse(userInfo) : null
}

// 移除用户信息
export function removeUserInfo() {
  Cookies.remove(USER_KEY)
}

// 清除所有认证相关的信息
export function clearAuthInfo() {
  removeToken()
  removeUserInfo()
}

// 设置 impactType 到 cookie
export function setImpactType(impactType, expires = 7) {
  Cookies.set(IMPACT_TYPE_KEY, impactType, { expires: expires })
}

// 从 cookie 中获取 impactType
export function getImpactType() {
  return Cookies.get(IMPACT_TYPE_KEY)
}

// 移除 impactType
export function removeImpactType() {
  Cookies.remove(IMPACT_TYPE_KEY)
}

// 设置 impactCode 到 cookie
export function setImpactCode(impactCode, expires = 7) {
  Cookies.set(IMPACT_CODE_KEY, impactCode, { expires: expires })
}

// 从 cookie 中获取 impactCode
export function getImpactCode() {
  console.log('getImpactCode',Cookies.get(IMPACT_CODE_KEY))
  return Cookies.get(IMPACT_CODE_KEY)
}

// 移除 impactCode
export function removeImpactCode() {
  Cookies.remove(IMPACT_CODE_KEY)
}